﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using Shrestha.Data.EventModel;

namespace Shrestha.Charts
{
    public class DrawingManager
    {
        #region Private Variables for Graph

        private double coeffLat, coeffLng, coeffDate;
        private TriEventCollection _teCollection;
        private Canvas _canvasGraph;
        private Runner _r;

        #endregion

        public DrawingManager(Canvas canvasGraph, Runner r)
        {
            this._canvasGraph = canvasGraph;
            this._r = r;

            Refresh();
        }

        public void GetCanvasCoordinates(double Latitude, double Longitude, DateTime EventDate, out double X, out double Y)
        {
            double px2 = 0;
            double py2 = _canvasGraph.ActualHeight;

            double px3 = _canvasGraph.ActualWidth;
            double py3 = _canvasGraph.ActualHeight;

            double pty1, ptx1, pty2, ptx2;

            pty1 = py2 - ((Latitude - _r.Meta.MinLatitude) * coeffLat);
            ptx1 = px2;

            pty2 = py3 - ((Longitude - _r.Meta.MinLongitude) * coeffLng);
            ptx2 = px3;

            double m = (pty2 - pty1) / _canvasGraph.ActualWidth;

            double tempx = EventDate.Subtract(_r.Meta.MinEventDate).TotalDays * coeffDate;
            double tempy = m * tempx + pty1;

            X = tempx + ptx1;
            Y = tempy;
        }

        public void GetCanvasCoordinates(double Latitude, double Longitude, DateTime EventDate, out double X, out double Y, out double YLat, out double YLng)
        {
            double px2 = 0;
            double py2 = _canvasGraph.ActualHeight;

            double px3 = _canvasGraph.ActualWidth;
            double py3 = _canvasGraph.ActualHeight;

            double pty1, ptx1, pty2, ptx2;

            pty1 = py2 - ((Latitude - _r.Meta.MinLatitude) * coeffLat);
            ptx1 = px2;

            pty2 = py3 - ((Longitude - _r.Meta.MinLongitude) * coeffLng);
            ptx2 = px3;

            double m = (pty2 - pty1) / _canvasGraph.ActualWidth;

            double tempx = EventDate.Subtract(_r.Meta.MinEventDate).TotalDays * coeffDate;
            double tempy = m * tempx + pty1;

            X = tempx + ptx1;
            Y = tempy;
            YLat = pty1;
            YLng = pty2;
        }

        public double GetXFromEventdate(DateTime EventDate)
        {
            return EventDate.Subtract(_r.Meta.MinEventDate).TotalDays * coeffDate;
        }

        public double GetYFromLatitude(double Latitude)
        {
            return _canvasGraph.ActualHeight - ((Latitude - _r.Meta.MinLatitude) * coeffLat);
        }

        public double GetYFromLongitude(double Longitude)
        {
            return _canvasGraph.ActualHeight - ((Longitude - _r.Meta.MinLongitude) * coeffLng);
        }

        internal void Refresh()
        {
            coeffLat = _canvasGraph.ActualHeight / (_r.Meta.MaxLatitude - _r.Meta.MinLatitude);
            coeffLng = _canvasGraph.ActualHeight / (_r.Meta.MaxLongitude - _r.Meta.MinLongitude);
            coeffDate = _canvasGraph.ActualWidth / _r.Meta.MaxEventDate.Subtract(_r.Meta.MinEventDate).TotalDays;
        }
    }
}
